package com.example.eos.mapper;

import com.example.eos.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;

import java.util.Date;

@Mapper
public interface UserMapper {

    //通过用户id查询用户
    @Select("select userid, username, name, sex, labelnum, fllownum, fansnum, publishnum, creattime, lasttime from user where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    User findByUserid(String userid);

    //通过手机号查询用户
    @Select("select userid, phone, password from user where phone = #{phone,jdbcType=VARCHAR} and exist = true")
    User findByPhone(String phone);

    //新建用户
    @Insert("insert into user (userid, phone, password, username, name, sex, labelnum, fllownum, fansnum, publishnum, creattime, lasttime, exist) values (#{userid,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}," +
            " #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{labelnum,jdbcType=INTEGER}, #{fllownum,jdbcType=INTEGER}, #{fansnum,jdbcType=INTEGER}, #{publishnum,jdbcType=INTEGER}, #{creattime}, #{lasttime}, #{exist,jdbcType=BOOLEAN})")
    void save(User user);

    //更新用户
    @Update("update user set username = #{username,jdbcType=VARCHAR}, name = #{name,jdbcType=VARCHAR}, sex = #{sex,jdbcType=VARCHAR} where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void update(User user);

    //更新密码
    @Update("update user set password = #{password,jdbcType=VARCHAR} where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void updatePassword(User user);

    //通过用户id删除用户
    @Update("update user set exist = false where userid = #{userid,jdbcType=VARCHAR}")
    void deleteByUserid(String userid);

    //更新用户最近登录时间
    @Update("update user set lasttime = #{lasttime} where phone = #{phone,jdbcType=VARCHAR} and exist = true")
    void changeLasttimeByPhone(@Param("phone")String phone, @Param("lasttime")Date lasttime);

    //根据用户id +标签数
    @Update("update user set labelnum = labelnum+1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void addLabelnum(String userid);

    //根据用户id -标签数
    @Update("update user set labelnum = labelnum-1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void minusLabelnum(String userid);

    //根据用户id +关注数
    @Update("update user set fllownum = fllownum+1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void addFollownum(String userid);

    //根据用户id -关注数
    @Update("update user set fllownum = fllownum-1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void minusFollownum(String userid);

    //根据用户id +粉丝数
    @Update("update user set fansnum = fansnum+1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void addFansnum(String userid);

    //根据用户id -粉丝数
    @Update("update user set fansnum = fansnum-1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void minusFansnum(String userid);

    //根据用户id +作品数
    @Update("update user set publishnum = publishnum+1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void addPublishnum(String userid);

    //根据用户id -作品数
    @Update("update user set publishnum = publishnum-1 where userid = #{userid,jdbcType=VARCHAR} and exist = true")
    void minusPublishnum(String userid);

}
